package music

import (
	"context"
	"mime/multipart"
	"time"

	"go-weixin/app/music/musicapi/internal/svc"
	"go-weixin/app/music/musicapi/internal/types"
	"go-weixin/app/music/musicrpc/pb/proto/music"

	"github.com/zeromicro/go-zero/core/logx"
)

type MusicAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMusicAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MusicAddLogic {
	return &MusicAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MusicAddLogic) MusicAdd(req *types.MusicAddReq, file multipart.File, fileHeader *multipart.FileHeader) (resp *types.MusicAddResp, err error) {
	ctx, cannFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer cannFunc()
	if err != nil {
		return
	}
	fileByte := make([]byte, fileHeader.Size)
	file.Read(fileByte)
	res, err := l.svcCtx.MusicRpc.MusicAdd(ctx, &music.MusicAddReq{
		FileByte: fileByte,
		Filename: fileHeader.Filename,
		Size:     fileHeader.Size,
	})
	if err != nil {
		return nil, err
	}
	resp = new(types.MusicAddResp)
	resp.Domain = res.Domain
	resp.Md5 = res.Md5
	resp.Path = res.Path
	resp.Size = res.Size
	return
}
